import numpy as np

#随机超平面哈希族
class RandomHyperplaneLSH:
    def __init__(self, num_planes, dim):
        self.num_planes = num_planes
        self.dim = dim
        self.hyperplanes = np.random.randn(num_planes, dim)  # 生成随机超平面

    def hash(self, x):
        signs = np.dot(self.hyperplanes, x) >= 0  # 计算点x在每个超平面的一侧
        return ''.join(map(str, signs.astype(int)))  # 将每个超平面的结果连接成一个哈希码
    
#局部敏感哈希族
class MinHashLSH:
    def __init__(self, num_hashes, dim):
        self.num_hashes = num_hashes
        self.dim = dim
        self.hash_funcs = [self._generate_hash_func() for _ in range(num_hashes)]

    def _generate_hash_func(self):
        a = np.random.randint(1, self.dim)  # 随机选择哈希函数参数a
        b = np.random.randint(0, self.dim)  # 随机选择哈希函数参数b
        return lambda x: (a * x + b) % self.dim  # 哈希函数形式

    def hash(self, x):
        return [min_hash(x) for min_hash in self.hash_funcs]  # 对数据点进行多个哈希计算
    


#（以下可以不用看，删了都行）
# #废弃方法，算法雏形
# def algorithmTest1(input_Excel_path, output_Excel_path, is_single = False):
#     data_list,data_dict = excelUtil.readExcel(input_Excel_path)
#     #测试方法一：将tesor转换成常量，根据常量聚类，内部比较
#     embedder = BGEModel(config.model_version)
#     data_embeddings_list = []

#     key_data_embedding_dict = {}
#     #生成一个工单编号和文本向量组成的list
#     for data in data_list:
#         data_dict_embedding = {}
#         data_dict_embedding["id"] = data["id"]
#         data_embeddings =  embedder.encode(data["text"])  
#         data_dict_embedding["embedding"] = data_embeddings
#         data_embeddings_list.append(data_dict_embedding)

#         tmp_total_key = sum(data_embeddings)
#         #在这分类，计算所有1024维度累加的和
#         tmp_total_key = math.ceil(tmp_total_key )

#         if tmp_total_key not in key_data_embedding_dict.keys() :
#             key_data_embedding_dict [tmp_total_key] = [data_dict_embedding]
#         else : 
#             key_data_embedding_dict [tmp_total_key].append(data_dict_embedding)

#     threshold = config.threshold
#     category_dict = {}
#     tmp_num = 0

#     log_info.info("go in method")
#     for key,value_list in key_data_embedding_dict.items():
#         print("key {}  value num {}".format(str(key),str(len(value_list))))

#         if len(value_list) < 2:
#             continue

#         uf = unionFind.UnionFind(len(value_list))
        
#         for i in range(len(value_list)):
#             data_i = value_list[i]
#             embedding_i = data_i["embedding"]

#             for j in range(i+1,len(value_list)): 
#                 if uf.find(i) == uf.find(j):
#                     continue
#                 data_j = value_list[j]  
#                 embedding_j = data_j["embedding"]

#                 cos_scores_text = cos_sim(embedding_i, embedding_j)[0]
#                 if cos_scores_text > threshold  :
#                     uf.union(i,j)

#         for i in range(len(value_list)):
#             uf.record(i)

#         for key,tmp_value_list in uf.groups.items():
#             tmp_num = tmp_num + 1
#             category_dict[tmp_num] = []
#             for value in tmp_value_list:
#                 category_dict[tmp_num].append(value_list[value]["id"])

#     log_info.info("method end")

#     category_dict = deleteSingle(category_dict, is_single)

#     excelUtil.writeExcel(output_Excel_path, data_dict, category_dict)
#     return 0